package fun.liuyu2783.txmessage.common;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 返回值
 * @author liuyu2783
 */
public class Response<T> implements Serializable {

    /**
     * code码
     */
    private String code;

    /**
     * 提示
     */
    private String msg;

    /**
     * 时间戳
     */
    private String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"));

    /**
     * 结果
     */
    private T result;

    private Response() {

    }

    public Response(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Response(String code, String msg, T result) {
        this.code = code;
        this.msg = msg;
        this.result = result;
    }

    public static <T> Response<T> error(String code, String msg) {
        return new Response(code, msg);
    }

    public static <T> Response<T> error(ResultCode retCode) {
        return new Response<>(retCode.getCode(), retCode.getMessage());
    }

    public static <T> Response<T> success() {
        return new Response(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }

    public static <T> Response<T> success(T result) {
        return new Response(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), result);
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(String timestamp) {
        this.timestamp = timestamp;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    @Override
    public String toString() {
        return "Response{" +
                "code='" + code + '\'' +
                ", msg='" + msg + '\'' +
                ", timestamp='" + timestamp + '\'' +
                ", result=" + result +
                '}';
    }
}
